package com.titaniume.java2;

import org.junit.Test;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 一、构造器引用
 *   和方法引用类似：函数式接口的抽象方法的形参列表和构造器的形参列表一致
 *   抽象方法的返回值类型即为构造器所属的类
 * 二、数组引用
 *   可以把数组看成是一个特殊的类，则写法和构造器引用一致
 *
 * Created by shkstart
 */
public class ConstructorRefTest {
	//构造器引用
    //Supplier中的T get()
    //Employee 的空参构造器： Employee()
    @Test
    public void test1(){

        Supplier<Employee> supplier1 = new Supplier<Employee>() {
            @Override
            public Employee get() {
                return new Employee();
            }
        };
        System.out.println(supplier1.get());

        System.out.println("********************************");



        Supplier<Employee> supplier2 = () -> new Employee();
        System.out.println(supplier2.get());
	}

	//Function中的R apply(T t)
    @Test
    public void test2(){

        Function<Integer, Employee> function1 = id -> new Employee(id);
        Employee employee = function1.apply(1001);
        System.out.println(employee);

        System.out.println("********************************");

        Function<Integer, Employee> function2 = Employee::new;
        Employee employee2 = function2.apply(1001);
        System.out.println(employee2);

	}

	//BiFunction中的R apply(T t, U t, R r)
    @Test
    public void test3(){

        BiFunction<Integer,String,Employee> biFunction1 =(id,name) -> new Employee(id,name);
        Employee employee1 = biFunction1.apply(1002, "张三");
        System.out.println(employee1);

        System.out.println("********************************");

        BiFunction<Integer,String,Employee> biFunction2 = Employee::new;
        Employee employee2 = biFunction2.apply(1003, "李四");
        System.out.println(employee2);




    }

	//数组引用
    //Function中的R apply(T t)
    @Test
    public void test4(){

        Function<Integer,String [] > function1 = l -> new String[l];

        System.out.println(Arrays.toString(function1.apply(10)));


        System.out.println("********************************");


        Function<Integer,String [] > function2 = String [] :: new;
        System.out.println(Arrays.toString(function2.apply(20)));




    }
}
